﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace StreamPDF.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetPDF(string fileName)
        {
            try
            {
                var fileRepositoryLocation = StreamPDF.Properties.Settings.Default.PDFLocation;

                FileInfo fileInfo = new FileInfo(fileRepositoryLocation + fileName);

                if (fileInfo.Exists)
                {
                    FileStream fileStream = fileInfo.OpenRead();
                    Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
                    return File(fileStream, "application/pdf");
                }
                else
                {
                    throw new Exception(fileName + " does not exist.");
                }
            }
            catch (Exception ex)
            {
                EventLog myLog = new EventLog();
                myLog.Log = "Application";
                myLog.Source = "StreamPDF";
                myLog.WriteEntry("HomeController:GetPDF: FileName:" + fileName + "::" + ex.Message, EventLogEntryType.Warning);
                
                return RedirectToAction("Index");

            }

        }

    }
}
